﻿module Client

open Microsoft.SharePoint.Client
open System.Security;
open System.Net
open System

let Url = "https://geek.sharepoint.com/"

let Context = new ClientContext(Url)

Console.WriteLine("======================================================================================")
Console.WriteLine("If using SharePoint Online, do not forget to use in the console window: ")
Console.WriteLine("Client.SetCredentials \"username\" \"password\" ;;")
Console.WriteLine("======================================================================================")

let Load(item:'a) = 
    Context.Load(item)
    Context.ExecuteQuery()

let ExecuteQuery() =
    Context.ExecuteQuery()

let SetCredentials(usr:string)(pwd:string) = 
    let securePwd = new SecureString()
    for i in 0..securePwd.Length-1 do
        securePwd.RemoveAt(i)
    for char in pwd.ToCharArray() do
        securePwd.AppendChar(char)
    Console.WriteLine("===============================================================================")
    Console.WriteLine("Do not show your password to others! Clear window now: Right Click -> Clear All")
    Console.WriteLine("===============================================================================")
    Context.Credentials <- new SharePointOnlineCredentials(usr, securePwd)